//
//  GuideViewController.swift
//  yuejian
//
//  Created by yanziye on 2017/9/25.
//  Copyright © 2017年 gyy. All rights reserved.
//

import UIKit
class GuideViewController:UIViewController,UIScrollViewDelegate
{
    var numOfPages = 4
    override func viewDidLoad()
    {
        let frame = self.view.bounds
        //scrollView的初始化
        let scrollView=UIScrollView()
        scrollView.frame=self.view.bounds
        scrollView.delegate = self
        //为了能让内容横向滚动，设置横向内容宽度为3个页面的宽度总和
        scrollView.contentSize=CGSize(width: frame.size.width*CGFloat(numOfPages),height: frame.size.height)
        
        scrollView.isPagingEnabled=true
        scrollView.showsHorizontalScrollIndicator=false
        scrollView.showsVerticalScrollIndicator=false
        scrollView.scrollsToTop=false
        
        for i in 0..<numOfPages{
            let imgfile = "jianjie\(Int(i+1)).png"
            print(imgfile)
            let image = UIImage(named:"\(imgfile)")
            let imgView = UIImageView(image: image)
            imgView.frame=CGRect(x: frame.size.width*CGFloat(i),y: CGFloat(0),width: frame.size.width,height: frame.size.height)
            scrollView.addSubview(imgView)
        }
        
        scrollView.contentOffset = CGPoint.zero
        self.view.addSubview(scrollView)
    }
    //        contentSize（类型为CGSize，代表尺寸）：是scrollview可以滚动的区域，比如frame = (0 ,0 ,320 ,480) contentSize = (320宽 ,960高)，代表你的scrollview可以上下滚动，滚动区域为frame大小的两倍。
    //
    //        contentOffset（类型为CGPoint，代表坐标）：是scrollview当前显示区域顶点相对于frame顶点的偏移量，比如上个例子你拉到最下面，contentoffset就是(0 ,480)，也就是y偏移了480
    //
    //        contentInset是scrollview的contentview的顶点相对于scrollview的位置，例如你的contentInset = (0 ,100)，那么你的contentview就是从scrollview的(0 ,100)开始显示
    
    func scrollViewDidScroll(_ scrollView: UIScrollView)
    {
        print("scrolled:\(scrollView.contentOffset)")
        let thirdh = CGFloat(numOfPages-1) * self.view.bounds.size.width
        if(scrollView.contentOffset.x > thirdh)
        {
            let mainStoryboard = UIStoryboard(name:"Main", bundle:nil)
            let viewController: ViewController = mainStoryboard.instantiateViewController(withIdentifier: "mainstoryboard") as! ViewController
            self.present(viewController, animated: true, completion:nil)
            
        }
    }
}
